﻿using System;
using System.Data;
using Ntt.NttData.Been.CommonLibrary.Database.SqlServer;

namespace NTTDATA.BEENChina.CaseMonitor.ServerChecker
{
    [Serializable]
    public class Management:Server
    {
        protected override bool CheckRunnint()
        {
            //throw new NotImplementedException();

            logRecords_current = GetLogRecords();

            if (logRecords_current.Equals(logRecores_previous))
            {

                if (logRecords_current.Equals(0))
                    return false;

                int undiswk = GetUnDistriwkOrder();
                int unrec = GetUnReceiveOrder();

                if (undiswk > 0 || unrec > 0)
                { 
                    SaveRecores();
                    return false;
                }
            }

            SaveRecores();
            return true;
        }

        private int logRecores_previous, logRecords_current;

        private void SaveRecores()
        {
            logRecores_previous = logRecords_current;
        }

        private String GetDBconnString()
        {
            return String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", this.Address, DBName, UserId, PassWord);
        }

        private String GetLogDBconnString()
        {
            return String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", this.Address, LogDBName, UserId, PassWord);
        }

        private String GetUnReceiveSql()
        {
            return "SELECT COUNT(*)  FROM OrderQueue WITH(NOLOCK) WHERE receivetime IS NULL"; 
        }

        private int GetUnReceiveOrder()
        {

            int count = 0;

            try
            { 
                int.TryParse(new SqlHelper().ExecuteScalar(GetDBconnString(), CommandType.Text, GetUnReceiveSql()).ToString(), out count);

                Log.Console.Debug(string.Format("Ma Address:{1}{0}  UnReceiveOrder:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetUnReceiveSql()));



            }
             catch (Exception ex)
             {
                 Log.Console.Error(string.Format("Ma Address:{1}{0}  UnReceiveOrder:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetUnReceiveSql()));
                 Log.Console.Error("CaseChecker ServerChecker Management GetUnReceiveOrder", ex);
             }

            return count;
        }


        private String GetUnDistriwkSql()
        {
            return "SELECT COUNT(*)  FROM t_distriwk WITH(NOLOCK) WHERE distridate IS NULL";
             
        }


        private int GetUnDistriwkOrder()
        {

            int count = 0;
            try
            { 
                int.TryParse(new SqlHelper().ExecuteScalar(GetDBconnString(), CommandType.Text, GetUnDistriwkSql()).ToString(), out count);

                Log.Console.Debug(string.Format("Ma Address:{1}{0}  UnDistriwk:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetUnDistriwkSql()));
  

            }
            catch (Exception ex)
            {
                Log.Console.Error(string.Format("Ma Address:{1}{0}  UnDistriwk:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetUnDistriwkSql()));
                Log.Console.Error("CaseChecker ServerChecker Management GetUnDistriwkOrder", ex);
            }
            return count;
        }

        private String GetLogRecordsSql()
        {
            return "SELECT COUNT(*)  FROM LogRecorder WITH(NOLOCK)  "; 
        }

        private int GetLogRecords()
        {

            int count = 0;
            try
            {
                int.TryParse(new SqlHelper().ExecuteScalar(GetLogDBconnString(), CommandType.Text, GetLogRecordsSql()).ToString(), out count);
                Log.Console.Debug(string.Format("Ma Address:{1}{0}  LogRecords:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetLogRecordsSql()));
 
            }
            catch (Exception ex)
            {

                Log.Console.Error(string.Format("Ma Address:{1}{0}  LogRecords:{2}{0}  Sql{3}", Environment.NewLine, Address, count, GetLogRecordsSql()));
                Log.Console.Error("CaseChecker ServerChecker Management GetLogRecords", ex);
            }
            

            return count;
        }
    }
}
